package com.thinkit.bigdata.web.listener;

import com.thinkit.bigdata.core.util.es.elasticsearch.EsCrud;
import com.thinkit.bigdata.core.util.ip.IPUtil;
import com.thinkit.bigdata.core.util.thread.ThreadExecutorUtil;
import com.thinkit.bigdata.web.coremet.SystemStatic;
import com.thinkit.bigdata.web.model.BaseEntity;
import com.thinkit.bigdata.web.model.bus.BusVoiceDetails;
import com.thinkit.bigdata.web.model.bus.BusVoiceDetailsExample;
import com.thinkit.bigdata.web.model.config.SysStatusAtt;
import com.thinkit.bigdata.web.model.config.SysStatusAttExample;
import com.thinkit.bigdata.web.service.config.StatusAttService;
import com.thinkit.bigdata.web.service.listener.ServiceConfig;
import com.thinkit.bigdata.web.thread.Consumer;
import com.thinkit.bigdata.web.thread.Producer;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

@Component
public class MyApplicationListener implements ApplicationContextAware, ServletContextAware,
        InitializingBean,  ApplicationListener<ContextRefreshedEvent> {

    private static Logger logger = Logger.getLogger(MyApplicationListener.class);

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        logger.info("1 => StartupListener.setApplicationContext");
    }

    @Override
    public void setServletContext(ServletContext context) {
        logger.info("2 => StartupListener.setServletContext");
    }

    @Override
    public void afterPropertiesSet() {
        logger.info("3 => StartupListener.afterPropertiesSet");
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent evt) {
        String logTxt = "【初始化程序启动】";
        logger.info(logTxt + "es客户端启动");
        try {
            SystemStatic.crud = EsCrud.getInstance();
        } catch (UnknownHostException e) {
            logger.info(logTxt + "es客户端启动 - 异常");
        }

        ServiceConfig serviceConfig = evt.getApplicationContext().getBean(ServiceConfig.class);
        logger.info(logTxt + "【明细导出】【5G数据导出】异常数据检查");
        BusVoiceDetailsExample example = new BusVoiceDetailsExample();
        example.createCriteria().andLocalipEqualTo(IPUtil.getLocalIP()).andStatusNotEqualTo(BaseEntity.VOICE_EXPORT_YES);
        List<BusVoiceDetails> details = serviceConfig.getDetailsService().selectByExample(example);
        for (BusVoiceDetails detail : details) {
            //将任务放到阻塞队列中
            ThreadExecutorUtil.executeTask(new Producer(SystemStatic.queue, detail));
        }

        logger.info(logTxt + "话单详情导出数据任务 - 消费者启动");
        SystemStatic.queue = new LinkedBlockingQueue<>(10);
        ThreadExecutorUtil.executeTask(new Consumer(SystemStatic.queue, serviceConfig.getDetailsService()));
        logger.info("4.1 => MyApplicationListener.onApplicationEvent");
        if (evt.getApplicationContext().getParent() == null) {
            logger.info("4.2 => MyApplicationListener.onApplicationEvent");
        }

        logger.info(logTxt + "mysql数据常量初始化");
        //挂机放
        Map<String, String> map = new HashMap<>();
        SysStatusAttExample exampleHook = new SysStatusAttExample();
        exampleHook.createCriteria().andTypeEqualTo("audio_onhook");
        List<SysStatusAtt> onHookList = serviceConfig.getStatusAttService().selectByExample(exampleHook);
        for (SysStatusAtt onHook : onHookList) {
            map.put(onHook.getValue(), onHook.getName());
        }
        SystemStatic.ONHOOK_MAP = map;
        //满意度
        map.clear();
        SysStatusAttExample exampleSatisfaction = new SysStatusAttExample();
        exampleSatisfaction.createCriteria().andTypeEqualTo("audio_satisfaction");
        List<SysStatusAtt> satisfactionList = serviceConfig.getStatusAttService().selectByExample(exampleSatisfaction);
        for (SysStatusAtt satisfaction : satisfactionList) {
            map.put(satisfaction.getValue(), satisfaction.getName());
        }
        SystemStatic.SATISFACTION_MAP = map;
        //客户星级
        map.clear();
        SysStatusAttExample exampleCustomerlevel = new SysStatusAttExample();
        exampleCustomerlevel.createCriteria().andTypeEqualTo("audio_customer_level");
        List<SysStatusAtt> customerLevelList = serviceConfig.getStatusAttService().selectByExample(exampleCustomerlevel);
        for (SysStatusAtt customerLevel : customerLevelList) {
            map.put(customerLevel.getValue(), customerLevel.getName());
        }
        SystemStatic.CUSTOMERLEVEL_MAP = map;
    }

}
